#Boa:Dialog:Dialog1

import wx
import wx.grid
import consultasBD

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1CARGAR, wxID_DIALOG1DIFERENCIAS, wxID_DIALOG1GRID1, 
 wxID_DIALOG1INPTC, wxID_DIALOG1STATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(6)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(244, 120), size=wx.Size(578, 501),
              style=wx.DEFAULT_DIALOG_STYLE, title='Control de Inventario')
        self.SetClientSize(wx.Size(562, 465))

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(8, 56), size=wx.Size(544, 360),
              style=0)

        self.inptc = wx.TextCtrl(id=wxID_DIALOG1INPTC, name='inptc',
              parent=self, pos=wx.Point(56, 8), size=wx.Size(100, 21), style=0,
              value='')

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='PTC:', name='staticText1', parent=self, pos=wx.Point(24,
              8), size=wx.Size(24, 13), style=0)

        self.cargar = wx.Button(id=wxID_DIALOG1CARGAR, label='Cargar',
              name='cargar', parent=self, pos=wx.Point(184, 8), size=wx.Size(88,
              24), style=0)
        self.cargar.Bind(wx.EVT_BUTTON, self.OnCargarButton,
              id=wxID_DIALOG1CARGAR)

        self.diferencias = wx.Button(id=wxID_DIALOG1DIFERENCIAS,
              label='Arreglar Diferencias', name='diferencias', parent=self,
              pos=wx.Point(368, 424), size=wx.Size(115, 32), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.padre = parent
        self.indiceGrilla = 1
        
        self.sucursal = self.padre.sucursalNro
        self.columnas =['Codigo','Descripcion','Talle','Color',' INVENTARIO  ', '  STOCK  ','  DIFERENCIA  ']
        
        self.inptc.SetFocus()
        self.grid1.CreateGrid(1000,len(self.columnas))
    
        for i,col in enumerate(self.columnas): 
            self.grid1.SetColLabelValue(i, col)
            
        self.grid1.AutoSizeColumn(0,True)
        self.grid1.AutoSizeRow(0,True)
        self.grid1.AutoSizeColumn(1,True)
        self.grid1.AutoSizeColumn(2,True)
        self.grid1.AutoSizeColumn(3,True)
        self.grid1.AutoSizeColumn(4,True)
        self.grid1.AutoSizeColumn(5,True)
        self.grid1.AutoSizeColumn(6,True)
        
        for x in xrange(1000):
            for i in xrange(len(self.columnas)):
                    self.grid1.SetReadOnly(x,i,True)
    
    def mostrar_ptc_en_grilla(self, fila, resultado):
        '''Dado un ptc se muestra en una fila de la grilla'''
        self.grid1.SetCellValue(fila, 0, str(resultado['ptc'])) #descri
        #self.grid1.SetCellValue(fila, 1, str(resultado['Descripcion'])) #descri
        #self.grid1.SetCellValue(fila, 2, resultado['talle']) #talle
        #self.grid1.SetCellValue(fila, 3, resultado['color']) #color
        
        self.grid1.SetCellValue(fila, 4, str(1)) #inventario
        
        cantidad = int(resultado["stock"]) - 1
        self.grid1.SetCellValue(fila, 5, str(resultado["stock"])) #stock
        self.grid1.SetCellValue(fila, 6, str(cantidad)) #diferencia
        
        #incrementa el indice de la grilla
        self.indiceGrilla=self.indiceGrilla+1
    
    def _limpiar_grilla(self):        
        for fil in range(60):
            utiles.colorear_fila(self.grid1, fil, utiles.BLANCO)
            for col in range(6):            
                self.grid1.SetCellValue(fil, col, "")                
    

    def incrementaInventario(self,ptc, fila):
        '''Dada una fila se incrementa en 1 la cantidad''' 
        cantidad = int(self.grid1.GetCellValue(fila,4)) + 1
        self.grid1.SetCellValue(fila, 4, str(cantidad))
        
        dif = int(self.grid1.GetCellValue(fila, 5))- int(cantidad)
        self.grid1.SetCellValue(fila, 6, str(dif))



    def OnCargarButton(self, event):
        ptc = self.inptc.GetValue()
        busca = ""
        incremento=0
        for x in xrange(1000):
            busca = self.grid1.GetCellValue(x,0)
            if busca==ptc:
                self.incrementaInventario(ptc,x)
                incremento=1
                break
        
        if incremento== 0:
            SQL = "SELECT * FROM stock WHERE ptc='%s'" % (ptc)
            resultados = consultasBD.ordenCrudaResultDict(SQL)
    
            for r in resultados:
                self.mostrar_ptc_en_grilla( self.indiceGrilla, resultados)
                self.inptc.ChangeValue("")
                break
            
